package com.dailyblue.smbms.controller;


import com.alibaba.fastjson2.JSONArray;
import com.dailyblue.smbms.bean.PageInfo;
import com.dailyblue.smbms.bean.User;
import com.dailyblue.smbms.service.UserService;
import com.dailyblue.smbms.service.impl.UserServiceImpl;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("/user_search")
public class UserSearchController extends HttpServlet {

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String name = request.getParameter("name");
        String role = request.getParameter("role");
        String p = request.getParameter("page");
        int page = 1;
        if (p != null) {
            page = Integer.parseInt(p);
        }
        UserService service = new UserServiceImpl();
        PageInfo<List<User>> pageInfo = service.findByNameAndRole(name, role, page);
        response.setContentType("application/json;charset=utf-8");
        PrintWriter out = response.getWriter();
        String json = JSONArray.toJSONString(pageInfo);
        out.println(json);
    }
}
